from sqlalchemy import ForeignKey, CheckConstraint
from sqlalchemy.orm import Mapped, mapped_column, relationship
from database.models.base import Base, created_at

class WeightUpdate(Base):
    __tablename__ = "weight_updates"
    
    username: Mapped[int] = mapped_column(
        ForeignKey("users.username", ondelete="CASCADE", onupdate="CASCADE"), 
        primary_key=True,
    )
    time_stamp: Mapped[created_at] = mapped_column(primary_key=True)
    weight: Mapped[int]
    
    user: Mapped["User"] = relationship(  # noqa: F821
        back_populates="weight_updates",
    )
    
    __table_args__ = (
        CheckConstraint("weight > 0", name="check_weight_positive"),
    )
    